package cn.lyjuan.dwz_manage.core.java.util;


import cn.lyjuan.dwz_manage.core.java.cst.DwzCst;
import cn.lyjuan.dwz_manage.core.java.dto.DwzApiBean;
import com.google.gson.Gson;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;


public class ActionUtils
{
    private static Logger log = LogManager.getLogger(ActionUtils.class.getName());

    /**
     * 是否ajax请求
     */
    public static boolean isAjaxRequest(HttpServletRequest request)
    {
        if ("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))
                || request.getParameter("ajax") != null || request.getHeader("ajax") == "true")
        {
            return true;
        }

        if (request.getRequestURI().indexOf("/api/") > -1) return true;

        return false;
    }

    /**
     * 为请求和响应设置默认编码
     *
     * @param req
     */
    public static void setDefaultEncode(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            req.setCharacterEncoding(DwzCst.charset);
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        // TODO resp 为空
        resp.setCharacterEncoding(DwzCst.charset);
        resp.setContentType("text/html;charset=" + DwzCst.charset);
    }

    /**
     * 返回 Ajax 请求响应
     *
     * @param req  HTTP请求
     * @param resp HTTP响应
     * @param json 返回的JSON数据
     * @throws java.io.IOException
     */
    public static void returnAjax(HttpServletRequest req, HttpServletResponse resp, String json)
    {
        // 设置请求响应编码
        setDefaultEncode(req, resp);

        // 获取输出封装流
        PrintWriter out = null;
        try
        {
            out = resp.getWriter();

            // 返回信息
            log.debug("Method[returnAjax] response[" + json + "]");

            out.write(json);
            out.flush();
        } catch (IOException e)
        {
            log.error("Method[returnAjax] 获取输出流异常", e);
        }
    }

    /**
     * 返回后台信息
     *
     * @param req
     * @param resp
     * @param code
     * @param desc
     */
    public static void returnDwzAjax(HttpServletRequest req, HttpServletResponse resp, int code, String desc)
    {
        returnDwzAjax(req, resp, code, desc, null);
    }

    public static void returnDwzAjax(HttpServletRequest req, HttpServletResponse resp, int code, String desc, Object data)
    {
        String json = getDWZajaxReturn(code, desc, data);

        returnAjax(req, resp, json);
    }

    public static String getDWZajaxReturn(int code, String msg)
    {
        return getDWZajaxReturn(code, msg, null);
    }

    /**
     * 转换Dwz后台Ajax响应信息
     *
     * @param code
     * @param msg
     * @return
     */
    public static String getDWZajaxReturn(int code, String msg, Object data)
    {
        DwzApiBean dwz = new DwzApiBean(code, msg, data);

        Gson gson = new Gson();
        return gson.toJson(dwz);
    }
}